/**
 * Created by cjd on 2016/5/4.
 */
angular.module("app").controller("loginCtrl", ["$scope", "$http", '$timeout', "$state", "User", function ($scope, $http, $timeout, $state, User) {
    if(sessionStorage.userInfo){
        $state.go("home.welcome");//转跳到欢迎页
    }
    else{
        $scope.phone = "";// 账号
        $scope.password = "";//密码
        if (localStorage.remenberName) {//如果之前有记录账号，则显示账号
            $scope.remenberName = true;
            $scope.phone = localStorage.remenberName;
        }
        $scope.login = function () {//登录函数
            sessionStorage.clear();
            User.Login({}, {phone: $scope.phone, password: $scope.password}, function (data) {
                /*登录成功，并记录账号时，将账号记录到 localStorage
                 *如果取消勾选，清空localStorage
                 */
                if (data.statusCode == 0) {
                    sessionStorage.userInfo = JSON.stringify(data.data);
                    // $state.go("home.welcome");//转跳到欢迎页
                    $state.go("home.topic.list");
                } else {
                    alert("用户名密码错误!")
                }
            })
        }
    }
}]);